package com.xrz.sxm.aj.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
/**
 *
 * 类名：VersonUtil
 * 说明：获取APK文件的版本名字
 *
 */
public class VersonUtil {

	/**
	 *
	 * 功能：获取APK文件的版本名字
	 * @param ctx
	 * @param apkPath apk文件所在的路径
	 * @return
	 */
	public static String getAPKFileVersonName(Context ctx, String apkPath){
		String version = "";
		PackageManager pm = ctx.getPackageManager();
		PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
		if(info != null){
			ApplicationInfo appInfo = info.applicationInfo;
			String appName = pm.getApplicationLabel(appInfo).toString();
			String packageName = appInfo.packageName;  //得到安装包名称
			version=info.versionName;       //得到版本信息
		}
		return version;
	}

	/**
	 * 获取版本号
	 */
	public static String getVisionName(Context ctx) {
		String visionName = "";
		try {
			PackageManager pm = ctx.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), 0);
			visionName = pi.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return visionName;
	}

	/**
	 * 获取版本号
	 */
	public static int getVisionCode(Context ctx) {
		int visionName = 0;
		try {
			PackageManager pm = ctx.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), 0);
			visionName = pi.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return visionName;
	}
}
